#pragma once

#include <Socket.h>
#include <Channel.h>
#include <SocketOps.h>
#include <EventLoop.h>
#include <memory>

/*
    note:
        向EventLoop注册Channel可以参考acceptor
 */

class Acceptor
{
    using NewConnectionCallbak = std::function<void(int, const sockaddr_in &)>;

public:
    Acceptor(EventLoop *loop, sockaddr_in addr, bool reuseport);

    ~Acceptor();

    void listen();

    bool listening() { return listening_; }
    void setNewConnectionCallback(NewConnectionCallbak cb)
    {
        newConnectionCallbak_ = cb;
    }

private:
    void handleRead();

private:
    bool listening_;
    EventLoop *loop_;
    NewConnectionCallbak newConnectionCallbak_;
    // 注意fd与channel的声明顺序和构造函数初始化中的顺序
    Socket acceptSock_;
    std::unique_ptr<Channel> acceptorChannel_;
    int idleFd_; // 用于处理文件描述符耗尽的情况，暂时没有实现
};